package com.aivin;


import java.io.File;

public class GitTool {
    public static void main(String[] args) throws Exception {
        testBaseCmd();
    }

    public static void testBaseCmd() throws Exception {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
        processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
        File workDir = new File("./myGitPro/");
        if (!workDir.exists()){
            workDir.mkdirs();
        }

        // 设置工作目录
        processBuilder.directory(workDir);

        int exitCode = processBuilder.command("git" ,"remote" ,"-v" ).start().waitFor();
        System.out.println("远程仓库=" + exitCode + " (0代表成功) \n\n");

        int exitCode2 = processBuilder.command("git" ,"branch"  ).start().waitFor();
        System.out.println("本地分支=" + exitCode2 + " (0代表成功)\n\n");


        int exitCode3 = processBuilder.command("git" ,"pull"  ).start().waitFor();
        System.out.println("pull结果=" + exitCode3 + " (0代表成功)\n\n");

        // git push origin develop:develop
        // git push 远程仓库名 本地分支名:远程仓库名
    }


    public static void testClone()throws Exception {
        String repoUrl = "https://devcloud.szlanyou.com/gitlab/mobile_development_platform/Project/szly-23110/da/mall_group/config.git"; // 替换为你的仓库URL
        String destPath = "./myGitPro/config/";
        deleteFolder(new File(destPath));

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command( /*"bash","-c",*/ "git" ,"clone" ,repoUrl ,destPath);

        processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
        processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);

        Process process = processBuilder.start();
        int exitCode = process.waitFor();
        System.out.println("克隆结束=" + exitCode + " (0代表成功)");
    }


    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    file.delete();
                }
            }
        }
        folder.delete();
    }
}
